<template>
  <div class="appointment-statistics">
    <el-row :gutter="20">
      <!-- 统计卡片 -->
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card class="statistics-card" :body-style="{ padding: '20px' }">
          <div class="card-content">
            <div class="card-value">
              <span class="number">{{ card.value }}</span>
              <span class="unit">{{ card.unit }}</span>
            </div>
            <div class="card-title">{{ card.title }}</div>
            <div class="card-trend">
              <span :class="['trend', card.trend > 0 ? 'up' : 'down']">
                {{ Math.abs(card.trend) }}%
                <el-icon>
                  <CaretTop v-if="card.trend > 0" />
                  <CaretBottom v-else />
                </el-icon>
              </span>
              <span class="trend-text">较上月</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 预约趋势图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>预约趋势</span>
              <el-radio-group v-model="trendTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>

      <!-- 预约分布图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>预约分布</span>
              <el-select v-model="distributionType" size="small">
                <el-option label="按科目" value="subject" />
                <el-option label="按时段" value="timeSlot" />
                <el-option label="按状态" value="status" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="distributionChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 教练预约排名 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>教练预约排名</span>
              <el-select v-model="rankingTimeRange" size="small">
                <el-option label="本周" value="week" />
                <el-option label="本月" value="month" />
                <el-option label="本年" value="year" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="rankingChartRef"></div>
        </el-card>
      </el-col>

      <!-- 预约完成率 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>预约完成率</span>
              <el-select v-model="completionTimeRange" size="small">
                <el-option label="本周" value="week" />
                <el-option label="本月" value="month" />
                <el-option label="本年" value="year" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="completionChartRef"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 统计卡片数据
const statisticsCards = [
  {
    title: '今日预约',
    value: 35,
    unit: '个',
    trend: 16.7
  },
  {
    title: '本周预约',
    value: 186,
    unit: '个',
    trend: 12.5
  },
  {
    title: '完成率',
    value: 97.2,
    unit: '%',
    trend: 3.4
  },
  {
    title: '取消率',
    value: 2.8,
    unit: '%',
    trend: -1.8
  }
]

// 图表相关
const trendTimeRange = ref('month')
const distributionType = ref('subject')
const rankingTimeRange = ref('month')
const completionTimeRange = ref('month')

const trendChartRef = ref(null)
const distributionChartRef = ref(null)
const rankingChartRef = ref(null)
const completionChartRef = ref(null)

let trendChart = null
let distributionChart = null
let rankingChart = null
let completionChart = null

// 初始化趋势图
const initTrendChart = () => {
  trendChart = echarts.init(trendChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['科目二', '科目三']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '科目二',
        type: 'line',
        smooth: true,
        data: [15, 18, 22, 25, 20, 28, 32]
      },
      {
        name: '科目三',
        type: 'line',
        smooth: true,
        data: [10, 12, 15, 18, 16, 22, 25]
      }
    ]
  }
  trendChart.setOption(option)
}

// 初始化分布图
const initDistributionChart = () => {
  distributionChart = echarts.init(distributionChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '预约分布',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 65, name: '科目二' },
          { value: 35, name: '科目三' },
          { value: 45, name: '上午时段' },
          { value: 55, name: '下午时段' },
          { value: 70, name: '已完成' },
          { value: 20, name: '待确认' },
          { value: 10, name: '已取消' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  distributionChart.setOption(option)
}

// 初始化排名图
const initRankingChart = () => {
  rankingChart = echarts.init(rankingChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: ['李教练', '王教练', '张教练', '赵教练', '孙教练', '周教练', '吴教练', '郑教练']
    },
    series: [
      {
        name: '预约数量',
        type: 'bar',
        data: [38, 32, 30, 28, 25]
      }
    ]
  }
  rankingChart.setOption(option)
}

// 初始化完成率图
const initCompletionChart = () => {
  completionChart = echarts.init(completionChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '5%',
      left: 'center'
    },
    series: [
      {
        name: '预约状态',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: [
          { value: 85, name: '已完成' },
          { value: 10, name: '进行中' },
          { value: 5, name: '已取消' }
        ]
      }
    ]
  }
  completionChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  distributionChart?.resize()
  rankingChart?.resize()
  completionChart?.resize()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initDistributionChart()
  initRankingChart()
  initCompletionChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  trendChart?.dispose()
  distributionChart?.dispose()
  rankingChart?.dispose()
  completionChart?.dispose()
})
</script>

<style scoped>
.appointment-statistics {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.card-content {
  text-align: center;
}

.card-value {
  margin-bottom: 8px;
}

.card-value .number {
  font-size: 24px;
  font-weight: bold;
  margin-right: 4px;
}

.card-value .unit {
  font-size: 14px;
  color: #909399;
}

.card-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.card-trend {
  font-size: 12px;
}

.trend {
  display: inline-flex;
  align-items: center;
  margin-right: 4px;
}

.trend.up {
  color: #67c23a;
}

.trend.down {
  color: #f56c6c;
}

.trend-text {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 320px;
}
</style>